import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-30
 * Time: 19:27
 */

class MyThreadPool {
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();

    public void submit(Runnable runnable) throws InterruptedException { //像生产任务一样
        queue.put(runnable);
        
    }


    //此处实现一个固定线程数的线程池
    public MyThreadPool(int n) {
        for(int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                try {
                    while(true) {
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            //!!!不要忘记启动线程，上述只是创建线程了 需要启动线程~
            t.start();
        }
    }
}
public class ThreadDemo22 {
    public static void main(String[] args) throws InterruptedException {
            MyThreadPool pool = new MyThreadPool(10);
            for(int i = 0; i <= 1000; i++) {
                int number = i;
                pool.submit(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("hello" + number);
                    }
                });
            }
    }
}


